package org.proddevplm.dao.core;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.proddevplm.dao.api.IAdminMgmt;
import org.proddevplm.dao.data.administration.AccessLevel;
import org.proddevplm.dao.data.administration.Statistics;
import org.proddevplm.dao.util.HibernateUtil;

public class AdminMgmt implements IAdminMgmt{

	@Override
	public boolean setAccessLevels(int projectId, int roleId, int accessLevel) {
		
		AccessLevel al = new AccessLevel();
		al.setProjectId(projectId);
		al.setRoleId(roleId);
		al.setAccessLevel(accessLevel);
		
		HibernateUtil.currentSession().persist(al);
		
		return false;
	}

	@Override
	public boolean setStatistics(int projectId, String parameter, boolean status) {

		Statistics stats = new Statistics();
		stats.setProjectId(projectId);
		stats.setParameter(parameter);
		stats.setStatus(status);
		
		HibernateUtil.currentSession().persist(stats);
		
		return false;
	}

	@Override
	public Statistics getStatisticsByProjectandParameter(int projectId,
			String parameter) {

		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select stats from Statistics stats where stats.projectId=:projectId and stats.parameter=:parameter");
		q.setInteger("projectId", projectId);
		q.setString("parameter", parameter);
		
		return (Statistics)q.uniqueResult();
	}

	@Override
	public List<AccessLevel> getAccessLevelsByProjectId(int projectId) {

		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select al from AccessLevel al where al.projectId=:projectId");
		q.setInteger("projectId", projectId);
		return q.list();
	}

	@Override
	public boolean editAccessLevels(int accessLevel, AccessLevel al) {
		
		al.setAccessLevel(accessLevel);
		HibernateUtil.currentSession().merge(al);
		return false;
	}

	@Override
	public AccessLevel getAccessLevelById(int accessLevelId) {
		Session session = HibernateUtil.currentSession();
		Query q = session.createQuery("select al from AccessLevel al where al.id=:accessLevelId");
		q.setInteger("accessLevelId", accessLevelId);
		return (AccessLevel)q.uniqueResult();
	}
	
}